/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelReporteXUsuario.java
 *
 * Created on 04/05/2011, 11:32:34 AM
 */
package reportehorario.vista;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import reportehorario.controlador.ControladorEntrada;
import reportehorario.controlador.ControladorHorario;
import reportehorario.controlador.ControladorUsuario;
import reportehorario.recursos.beans.BeanEntradaXUsuarioXFecha;
import reportehorario.recursos.beans.BeanRecord;
import reportehorario.recursos.beans.BeanUsuario;

/**
 *
 * @author Metis Gaia 2010-02
 */
public class PanelReporteXUsuario extends javax.swing.JPanel {

    ControladorHorario controladorHorario = new ControladorHorario();
    ControladorUsuario controladorUsuario = new ControladorUsuario();
    ControladorEntrada controladorEntrada = new ControladorEntrada();
    ArrayList<BeanUsuario> listaUsuarios = null;
    int idUsuario = 0;
    String cadenaDiasJustificacion = "Los días son: ";

    /** Creates new form PanelReporteXUsuario */
    public PanelReporteXUsuario() throws Exception {
        listaUsuarios = controladorUsuario.obtenerUsuarios();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblHorario = new javax.swing.JTable();
        pnBusqueda = new javax.swing.JPanel();
        lblUsuario = new javax.swing.JLabel();
        cbxUsuario = new javax.swing.JComboBox();
        btnConsultar = new javax.swing.JButton();
        ccDesde = new org.freixas.jcalendar.JCalendarCombo();
        ccHasta = new org.freixas.jcalendar.JCalendarCombo();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblNombre = new java.awt.Label();

        setPreferredSize(new java.awt.Dimension(680, 480));

        tblHorario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "FECHA", "HORA ENTRADA", "HORA MARCADA", "MINUTOS TARDE", "JUSTIFICACION"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblHorario.getTableHeader().setReorderingAllowed(false);
        tblHorario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblHorarioMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblHorario);

        pnBusqueda.setBorder(javax.swing.BorderFactory.createTitledBorder("Busqueda"));

        lblUsuario.setText("Usuario");

        try {
            ArrayList<String> lista = new ArrayList<String>();
            lista.add("[SELECCIONE]");
            for (BeanUsuario x : listaUsuarios){
                lista.add(x.getUsuario());
            }

            String [] listaUsuarios = new String[lista.size()];
            lista.toArray(listaUsuarios);
            cbxUsuario.setModel(new javax.swing.DefaultComboBoxModel(listaUsuarios));
        } catch (Exception ex) {
            Logger.getLogger(Reporte.class.getName()).log(Level.SEVERE, null, ex);
        }

        btnConsultar.setText("Consultar");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        jLabel3.setText("Desde:");

        jLabel4.setText("Hasta:");

        javax.swing.GroupLayout pnBusquedaLayout = new javax.swing.GroupLayout(pnBusqueda);
        pnBusqueda.setLayout(pnBusquedaLayout);
        pnBusquedaLayout.setHorizontalGroup(
            pnBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBusquedaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnBusquedaLayout.createSequentialGroup()
                        .addComponent(lblUsuario)
                        .addGap(18, 18, 18)
                        .addComponent(cbxUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnBusquedaLayout.createSequentialGroup()
                        .addGroup(pnBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnConsultar)
                            .addGroup(pnBusquedaLayout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(ccDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ccHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(46, 46, 46))))
        );
        pnBusquedaLayout.setVerticalGroup(
            pnBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBusquedaLayout.createSequentialGroup()
                .addGroup(pnBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbxUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblUsuario))
                .addGap(18, 18, 18)
                .addGroup(pnBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(ccHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(ccDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnConsultar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 586, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(pnBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(pnBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(lblNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tblHorarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblHorarioMouseClicked
        // TODO add your handling code here:
        System.out.println("se presiono en la celda: " + tblHorario.getSelectedColumn());
        if (tblHorario.getSelectedColumn() == 5) {
            JOptionPane.showMessageDialog(null, cadenaDiasJustificacion.toString(), "Dias Justificados", JOptionPane.INFORMATION_MESSAGE);
        }
}//GEN-LAST:event_tblHorarioMouseClicked

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        try {
            // TODO add your handling code here:

            ArrayList<BeanRecord> listaBeanRecord = new ArrayList<BeanRecord>();
            java.sql.Date fechaDesde = new java.sql.Date(ccDesde.getDate().getTime());
            java.sql.Date fechaHasta = new java.sql.Date(ccHasta.getDate().getTime());
            if (cbxUsuario.getSelectedItem() != "[SELECCIONE]"){
                idUsuario = controladorUsuario.obtenerIdUsuario(cbxUsuario.getSelectedItem().toString());
            }
            
            //idUsuario ponemos igual a cero (0), para que haga una consulta de todos los usuarios en ese rango de fecha
            ArrayList<BeanEntradaXUsuarioXFecha> listaEntradas = controladorEntrada.obtenerEntradas(idUsuario, fechaDesde, fechaHasta);

            for (int i = 0; i < listaUsuarios.size(); i++) {
                if (!listaEntradas.isEmpty()) {
                    listaBeanRecord.add(controladorHorario.obtenerRecordXUsuarioXRangoFecha(listaUsuarios.get(i), listaEntradas));
                } else {
                    System.out.println("lista de entrada para este usuario esta vacia...");
                }

            }

            DefaultTableModel datos = (DefaultTableModel) tblHorario.getModel();
            datos.setRowCount(0);
            Object[] rowData;

            for (BeanRecord x : listaBeanRecord) {
                rowData = new Object[]{
                            x.getUsuario(),
                            x.getMin_tardanza(),
                            x.getDias_tardanza(),
                            x.getProm_minutos_usados(),
                            x.getDias_marcados(),
                            x.getDias_justificados()
                        };
                datos.addRow(rowData);
            }
        } catch (Exception ex) {
            Logger.getLogger(Reporte.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btnConsultarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConsultar;
    private javax.swing.JComboBox cbxUsuario;
    private org.freixas.jcalendar.JCalendarCombo ccDesde;
    private org.freixas.jcalendar.JCalendarCombo ccHasta;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private java.awt.Label lblNombre;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JPanel pnBusqueda;
    private javax.swing.JTable tblHorario;
    // End of variables declaration//GEN-END:variables
}
